import entity.Node;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class TreeUtils {

    static List<Node> buildTree(List<Node> list, Integer parentId) {
        List<Node> tree = new ArrayList<>();
        for (Node node : list) {
            if (node.parentId == parentId) {
                tree.add(findChild(node, list));
            }
        }
        return tree;
    }

    static Node findChild(Node node, List<Node> list) {
        for (Node n : list) {
            if (node.id == n.parentId){
                if (node.children == null) {
                    node.children = new ArrayList<Node>();
                }
                node.children.add(findChild(n, list));
            }
        }
        return node;
    }

}
